package com.swt.testdemo.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.swt.testdemo.config.ResponseBean;
import com.swt.testdemo.config.ResponseMessage;
import com.swt.testdemo.dao.SysDataLawTemplateDao;
import com.swt.testdemo.entity.SysDataLawTemplateEntity;
import com.swt.testdemo.form.CreateLawTemplateForm;
import com.swt.testdemo.form.EditSysDataLawTemplateForm;
import com.swt.testdemo.form.QueryTemplateForm;
import com.swt.testdemo.service.SysDataLawTemplateService;
import com.swt.testdemo.util.RedisUtil;
import com.swt.testdemo.util.ValidatorUtil;
import org.jsoup.Jsoup;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

/**
 * @author BleethNie
 * @version 1.0
 * @date 2020/11/16 9:37
 **/
@Service("SysDataLawTemplateService")
public class SysDataLawTemplateServiceImpl extends ServiceImpl<SysDataLawTemplateDao, SysDataLawTemplateEntity> implements SysDataLawTemplateService {
    @Autowired
    private SysDataLawTemplateDao sysDataLawTemplateDao;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public ResponseBean getTemplateList(QueryTemplateForm form) {

        List<SysDataLawTemplateEntity> sysDataLawTemplateEntities = sysDataLawTemplateDao.queryLawTemplate(form.getTemplateNameFilter());
        return new ResponseBean(200,"查询法律法规列表成功",sysDataLawTemplateEntities);
    }

    @Override
    public ResponseBean getTemplateInfo(Integer lawTemplateId) {
        String key = "sysLawTemplate: lawTemplateId:"+ lawTemplateId;
        SysDataLawTemplateEntity sysDataLawTemplateEntity = redisUtil.getObject(key, SysDataLawTemplateEntity.class);
        if (sysDataLawTemplateEntity == null){
            SysDataLawTemplateEntity dataLawTemplateEntity = this.getById(lawTemplateId);
            redisUtil.setObject(key,dataLawTemplateEntity,RedisUtil.DEFAULT_EXPIRE_TIME);
        }

        return new ResponseBean(200,"查询成功",sysDataLawTemplateEntity);
    }

    @Override
    public ResponseBean createTemplate(CreateLawTemplateForm form) {
        SysDataLawTemplateEntity sysDataLawTemplateEntity = new SysDataLawTemplateEntity();
        BeanUtils.copyProperties(form,sysDataLawTemplateEntity);
        String sampleUrl = form.getSampleUrl();
        if (!StrUtil.isEmpty(sampleUrl)){
            try {
                String html = Jsoup.connect(sampleUrl).get().html();
                sysDataLawTemplateEntity.setSampleHtml(html);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        DateTime current = DateUtil.date();
        sysDataLawTemplateEntity.setCreatedTime(current);
        sysDataLawTemplateEntity.setUpdateTime(current);
        this.save(sysDataLawTemplateEntity);

        return new ResponseBean(200,"创建法律法规模板成功",sysDataLawTemplateEntity);
    }

    @Override
    public ResponseMessage editTemplate(EditSysDataLawTemplateForm form) {
        //参数校验
        ValidatorUtil.validateEntity(form);

        String formSampleUrl = form.getSampleUrl();

        SysDataLawTemplateEntity sysDataLawTemplateEntity = new SysDataLawTemplateEntity();

        String sampleUrl = sysDataLawTemplateEntity.getSampleUrl();

        BeanUtils.copyProperties(form,sysDataLawTemplateEntity);

        DateTime current = DateUtil.date();
        sysDataLawTemplateEntity.setCreatedTime(current);
        sysDataLawTemplateEntity.setUpdateTime(current);

        //更新url
        boolean equals = StrUtil.equals(formSampleUrl, sampleUrl);

        if (!equals){
            if (!StrUtil.isEmpty(formSampleUrl)){
                try {
                    String html = Jsoup.connect(formSampleUrl).get().html();
                    sysDataLawTemplateEntity.setSampleHtml(html);

                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

        }

        this.updateById(sysDataLawTemplateEntity);

        return new ResponseMessage(200,"编辑法律法规模板成功");
    }

    @Override
    public ResponseMessage delTemplate(Integer dataTemplateId) {
        String key ="sysLawTemplate: lawTemplateId:"+dataTemplateId;
        redisUtil.del(key);

        //检查删除的模板是否存在
        SysDataLawTemplateEntity sysDataLawTemplateEntity = this.getById(dataTemplateId);
        if (sysDataLawTemplateEntity == null){
           return new ResponseMessage(500,"删除失败，请检查删除的模板是否存在");
        }

       this.removeById(dataTemplateId);
        return new ResponseMessage(200,"删除成功");
    }
}
